package com.ffapp.demo.bean;

import android.text.TextUtils;

import com.ffapp.demo.R;
import com.ffapp.demo.basis.BasisActivity;
import com.ffapp.demo.basis.Constants;
import com.ffapp.demo.other.PdfScanActivity;

import java.io.File;
import my.FileUtils;
import my.LogUtil;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;

public class ProjectFileData extends BasisBean {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * id : fce5bd0ad322465ab88e4d3ce41ad72c
	 * fileName : 9112.jpg
	 * filePath : http://192.168.25.236/xf_ent/tmchkfault/2019/01/3773ae33-706c-4a8b-8ced-0ce9cbb8f332.jpg
	 * fileType : 20
	 * fileExt : jpg
	 * fileOrder : 0
	 * createUser : xmadmin
	 * createTime : 2019-01-16 11:01:11
	 * updateUser : xmadmin
	 * updateTime : 2019-01-16 11:01:11
	 * isDelete : 0
	 */

	private String id;
	private String fileName;
	private String filePath;
	private String fileType;
	private String fileExt;
	private int fileOrder;
	private String createUser;
	private String createTime;
	private String updateUser;
	private String updateTime;
	private String isDelete;


	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFilePath() {
		return filePath;
	}
	public String getFileUrl() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public String getFileExt() {
		return fileExt;
	}

	public void setFileExt(String fileExt) {
		this.fileExt = fileExt;
	}

	public int getFileOrder() {
		return fileOrder;
	}

	public void setFileOrder(int fileOrder) {
		this.fileOrder = fileOrder;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}

	public String getIsDelete() {
		return isDelete;
	}

	public void setIsDelete(String isDelete) {
		this.isDelete = isDelete;
	}

	public int getImgresByname(){
		if(fileName.toLowerCase().endsWith(".pdf")){
			return R.drawable.file_icon_pdf;
		}else if(fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".png") || fileName.toLowerCase().endsWith(".jpeg") ){
			return R.drawable.file_icon_pic;
		}else if(fileName.toLowerCase().endsWith(".rar") || fileName.toLowerCase().endsWith(".zip")  ){
			return R.drawable.file_icon_rar;
		}else if(fileName.toLowerCase().endsWith(".doc") || fileName.toLowerCase().endsWith(".docx")  ){
			return R.drawable.file_icon_word;
		}else {
			return R.drawable.file_icon_other;
		}
	}

	public boolean isFileImg(){
		if(TextUtils.isEmpty(filePath))return false;
		if(filePath.startsWith("http"))
			return  FileUtils.getMIMETypeFromUrl(filePath).startsWith("image/");
		return  FileUtils.getMIMEType(new File(filePath)).startsWith("image/");
//		if(fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".png") || fileName.toLowerCase().endsWith(".jpeg") || fileName.toLowerCase().endsWith(".gif") ){
//			return true;
//		}
//		return false;
	}
	public boolean isFileVideo(){
//		LogUtil.i("isFileVideo----》"+filePath);
		if(!TextUtils.isEmpty(fileType) && fileType.equals("Video")){
			return true;
		}
		if(TextUtils.isEmpty(filePath))return false;
		if(filePath.startsWith("http"))
			return  FileUtils.getMIMETypeFromUrl(filePath).startsWith("video/");
		return  FileUtils.getMIMEType(new File(filePath)).startsWith("video/");
//		if(fileName.toLowerCase().endsWith(".mp4") || fileName.toLowerCase().endsWith(".mpeg") || fileName.toLowerCase().endsWith(".3gp") || fileName.toLowerCase().endsWith(".gif") ){
//			return true;
//		}
//		return false;
	}


	public static boolean isUrlFile(String url){
		String filetype = FileUtils.getMIMETypeFromUrl(url);
		if(filetype.startsWith("application/msword"))return true;
		if(filetype.startsWith("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))return true;
		if(filetype.startsWith("application/vnd.ms-excel"))return true;
		if(filetype.startsWith("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))return true;
		if(filetype.startsWith("application/vnd.ms-powerpoint"))return true;
		if(filetype.startsWith("application/vnd.ms-works"))return true;
		if(filetype.startsWith("text/plain"))return true;
		return  false;
	}

	public String getLocalFileUrl() {
		File file = new File(Constants.DIR_TEMP);
		if (!file.exists()) {
			file.mkdirs();
		}
		return Constants.DIR_TEMP + fileName;
	}
//	public String getLocalFileUrlOnline() {
////        return Constants.DIR_FILE +FileUtils.getFileNameFromUrl(absUrl);
//		return Constants.DIR_FILE +FileUtils.getFileNameFromUrlSimple(fileUrl);
////        File file = new File(Constants.DIR_FILE);
////        if (!file.exists()) {
////            file.mkdirs();
////        }
////        return Constants.DIR_FILE + srcFile;
//	}

	public static void DownloadAndOpen(String url, BasisActivity mContext){
		ProjectFileData projectFileData = new ProjectFileData();
		projectFileData.setFilePath(url);
		projectFileData.setFileName(FileUtils.getFileNameFromUrlSimple(url));
		projectFileData.openOnline(mContext);
	}

	public boolean openOnline(BasisActivity mContext) {

		if (TextUtils.isEmpty(filePath)) {
			mContext.showToast("文档内容不存在。");
			return false;
		}

//		if (!checkWpsInstall(mContext)) {
//			return false;
//		}

		String filePath = getLocalFileUrl();
		File file = new File(filePath);
		if (file.exists()) {
			LogUtil.i("打开文件");
//			FileUtils.openFileWps(file, mContext);
			FileUtils.openFile(file,mContext);
			return true;
		} else {
//            makeFile();
//            FileUtils.openFileWps(file, mContext);
			download(mContext,true);
		}
		return false;
	}
	public boolean saveToSD(BasisActivity mContext) {

		if (TextUtils.isEmpty(filePath)) {
			mContext.showToast("文档内容不存在。");
			return false;
		}

//		if (!checkWpsInstall(mContext)) {
//			return false;
//		}

		String filePath = getLocalFileUrl();
		File file = new File(filePath);
		if (file.exists()) {
			LogUtil.i("打开文件--保存");
//			FileUtils.openFileWps(file, mContext);
//			FileUtils.openFile(file,mContext);
			mContext.showToast("文件已下载，已保存至"+getLocalFileUrl());
			return true;
		} else {
//            makeFile();
//            FileUtils.openFileWps(file, mContext);
			download(mContext,false);
		}
		return false;
	}

//	private boolean checkWpsInstall(final BasisActivity mContext) {
//
//		if (IntentUtils.checkAppInstalled("cn.wps.moffice_eng", mContext)) {
//			return true;
//		} else {
//			final MaterialDialog dialog = new MaterialDialog(mContext);
//			dialog.content("您未安装文档阅读软件，是否立即下载安装?")
//					.showAnim(BasisApp.bas_in)
//					.dismissAnim(BasisApp.bas_out)
//					.widthScale(0.65f)
//					.show();
//			dialog.setOnBtnClickL(new OnBtnClickL() {
//				@Override
//				public void onBtnClick() {
//					dialog.dismiss();
//				}
//			}, new OnBtnClickL() {
//				@Override
//				public void onBtnClick() {
//					DownAPKService.DownloadAndShowNotifition(mContext, "WPS", "http://kad.www.wps.cn/wps/download/android/kingsoftoffice_2052/moffice_2052_wpscn.apk");
////                  IntentUtils.ViewUrl(mContext,"http://kad.www.wps.cn/wps/download/android/kingsoftoffice_2052/moffice_2052_wpscn.apk");
//					dialog.dismiss();
//				}
//			});
//		}
//		return false;
//	}


	private void download(final BasisActivity mContext, final boolean isopen) {
		final String[] mAllowedContentTypes = new String[]{".*"};
		mContext.showLoadingDialog("正在下载");
		MyHttpUtils.download(getFileUrl(),Constants.DIR_TEMP,fileName,new MyHttpListener() {

			@Override
			public void onProgress(int httpWhat, long bytesWritten, long totalSize) {
				super.onProgress(httpWhat, bytesWritten, totalSize);
				LogUtil.i(MyHttpUtils.tag,"onProgress--bytesWritten/totalSize->"+bytesWritten+"/"+totalSize);
			}

			@Override
			public void onSuccess(int httpWhat, Object result) {
				if(mContext.isFinishing())return;;
				LogUtil.i(MyHttpUtils.tag,"文件下载成功");
				BasisBean basisBean = (BasisBean) result;
				if(isopen){
					LogUtil.i(MyHttpUtils.tag,"onSuccess---下载文件成功--->"+basisBean.getResultData());
					if(basisBean.getResultData().toLowerCase().endsWith(".pdf")){
						PdfScanActivity.toPdfScanActivity(mContext,basisBean.getResultData(),"");
					}else {
						FileUtils.openFile(new File(basisBean.getResultData()), mContext);
					}
				}else {
					mContext.showToast("下载成功，已保存至"+basisBean.getResultData());
				}
			}

			@Override
			public void onFailure(int httpWhat, Object result) {
				if(mContext.isFinishing())return;;
//				super.onFailure(httpWhat, result);
				LogUtil.i(MyHttpUtils.tag,"文件下载失败");
				mContext.showToast("下载失败，请检查网络！");
			}

			@Override
			public void onFinish(int httpWhat) {
				if(mContext.isFinishing())return;
				mContext.dismissProgress();
			}
		});
	}

}
